package com.example.mediarecorderdemo;

import android.media.SoundPool;
import android.media.SoundPool.Builder;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.LinearLayoutManager;
import com.example.mediarecorderdemo.databinding.FragmentPlayAudioBinding;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class PlayAudioFragment extends Fragment {
    private FragmentPlayAudioBinding mPlayAudioBinding;
    private List<Sound> mDataList = new ArrayList();
    private SoundPool mSoundPool;

    public PlayAudioFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mPlayAudioBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_play_audio, container, false);
        mPlayAudioBinding.setLifecycleOwner(requireActivity());
        return mPlayAudioBinding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(requireContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mPlayAudioBinding.recyclerView.setLayoutManager(linearLayoutManager);
        mSoundPool = (new Builder()).setMaxStreams(5).build();
        mDataList.add(new Sound("a1", mSoundPool.load(requireContext(), R.raw.a1, 1)));
        mDataList.add(new Sound("a2", mSoundPool.load(requireContext(), R.raw.a2, 1)));
        mDataList.add(new Sound("a3", mSoundPool.load(requireContext(), R.raw.a3, 1)));
        mDataList.add(new Sound("a4", mSoundPool.load(requireContext(), R.raw.a4, 1)));
        mDataList.add(new Sound("a5", mSoundPool.load(requireContext(), R.raw.a5, 1)));
        AudioAdapter audioAdapter = new AudioAdapter(mDataList, requireContext());
        audioAdapter.setOnRecyclerItemClickListener((position, name) -> {
            Toast.makeText(requireContext(), name, Toast.LENGTH_SHORT).show();
            Sound sound = mDataList.get(position);
            mSoundPool.play(sound.getSoundId(), 0.5F, 0.5F, 1, 0, 1.0F);
        });
        mPlayAudioBinding.recyclerView.setAdapter(audioAdapter);
        mPlayAudioBinding.btnJump.setOnClickListener((view) -> {
            NavController navController = Navigation.findNavController(view);
            navController.navigate(R.id.action_playAudioFragment2_to_cameraXFragment);
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Iterator var1 = mDataList.iterator();

        while(var1.hasNext()) {
            Sound sound = (Sound)var1.next();
            mSoundPool.unload(sound.getSoundId());
        }

        mSoundPool.release();
    }

    static class Sound {
        private String name;
        private int soundId;

        public Sound(String name, int soundId) {
            this.name = name;
            this.soundId = soundId;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getSoundId() {
            return soundId;
        }

        public void setSoundId(int soundId) {
            this.soundId = soundId;
        }
    }
}